<template>
  <carrier :title="title">
    <div slot="top">
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" width="30%" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="searchOrder"></el-button>
          </el-input>
        </el-col>
      </el-row>
    </div>
    <div slot="count">
      <!-- 表格展示 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column label="#" type="index">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80">
        </el-table-column>
        <el-table-column prop="order_fapiao_title" label="发票抬头" width="80">
        </el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="100">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.order_pay == 0">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80">
        </el-table-column>
        <el-table-column label="下单时间" width="200">
          <template v-slot="scope">
            <span>{{scope.row.create_time | dateFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="260">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editById(scope.row.order_id)">修改地址</el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgress">查看物流</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改地址对话框 -->
      <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="dialogClose">
        <el-form ref="editForm" :model="addressForm" label-width="100px" :rules="addressFormRules">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader v-model="addressForm.address1" :options="cityData" :props="{ expandTrigger: 'hover', value: 'value', label: 'label' }"></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 物流信息对话框 -->
      <el-dialog title="物流信息" :visible.sync="progressVisible" width="50%">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
            type="success">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[6, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </carrier>
</template>

<script>
  import Carrier from 'views/home/Carrier.vue'

  import { getHomeData3 } from 'network/home.js'

  import cityData from 'common/cityData.js'

  export default {
    name: 'Order',
    components: {
      Carrier
    },
    data() {
      return {
        title: ['订单管理', '订单列表'],
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 6
        },
        orderList: [],
        total: 0,
        editDialogVisible: false,
        // 保存所填写的地址信息
        addressForm: {
          address1: [],
          address2: ''
        },
        // 全国城市数据信息的存储
        cityData,
        addressFormRules: {
          address1: [
            { required: true, message: '请选择相应的省市区/县', trigger: 'blur' }
          ],
          address2: [
            { required: true, message: '请填写详细地址', trigger: 'blur' }
          ]
        },
        progressVisible: false,
        // 快递信息的存储
        progressInfo: []
      }
    },
    created() {
      this.getOrderList();
    },
    methods: {
      getOrderList() {
        getHomeData3({
          url: 'orders',
          params: this.queryInfo
        })
          .then(res => {
            // console.log(res);
            if (res.meta.status !== 200) {
              return this.$message({
                type: 'error',
                message: res.meta.msg
              })
            }
            this.total = res.data.total;
            this.orderList = res.data.goods;
          })
      },
      handleSizeChange(size) {
        this.queryInfo.pagesize = size;
        this.getOrderList();
      },
      handleCurrentChange(current) {
        this.queryInfo.pagenum = current;
        this.getOrderList();
      },
      // 打开修改地址对话框
      editById(id) {
        this.editDialogVisible = true;
      },
      dialogClose() {
        this.$refs.editForm.resetFields();
      },
      // 快递信息数据请求
      showProgress() {
        getHomeData3({
          url: '/kuaidi/1106975712662'
        })
        .then(res => {
          if (res.meta.status !== 200) {
              return this.$message({
                type: 'error',
                message: res.meta.msg
              })
            }
          this.progressInfo = res.data
        })
        .catch(err => {
          this.$message.error(new Error('获取数据失败，请稍候重试'))
        })
        this.progressVisible = true;
      },
      // 搜索功能的实现
      searchOrder() {
        this.getOrderList();
      }
    },
  }
</script>

<style scoped>
  .el-pagination {
    margin-top: 15px;
  }
  .el-cascader {
    width: 60%;
  }
</style>